var notifInterval, pesanInterval;

function initNotif(){
    $('#list-notif').html('');
    $.getJSON('includes/sso/cek-notif.php?act=1', function(data) {
        $.each(data,function(i,item){
            //var listComment = "<input id='id-pengisian' type='hidden' value='"+item.idPengadaan+"' />";
            //listComment += "<input class='id-jenis' type='hidden' value='"+item.idJenis+"' />";
            var classNotifNotif = "";
            var astKomen = "";
            if(item.statusNotif=="0"){
                var classNotifNotif = "class='blm-baca'";
            }
            //alert(classNotifKomen);
            $('#list-notif').append("<li "+classNotifNotif+" data-notif="+item.idNotif+"><a class='item-notif' href='"+item.url+"'><img class='pull-left' src='"+item.gambar+"'><span class='sender'>"+item.sender+"</span><br /><p>"+item.subjek+"</p><span class='pull-right'>"+item.tgl+"</span><br/></a></li>");
        });
        setInterval('cekNotif()', 5000);
    });
}

function cekListNotif(){
    $.getJSON('includes/sso/cek-notif.php?act=3', function(data) {
        $.each(data,function(i,item){
            var classNotifNotif = "";
            var astKomen = "";
            if(item.statusNotif=="0"){
                var classNotifNotif = "class='blm-baca'";
            }
            var curNotif = $('#list-notif').find('li[data-notif='+item.idNotif+']');
            if (curNotif.length < 1) {
                $('#list-notif').prepend("<li "+classNotifNotif+" data-notif="+item.idNotif+"><a class='item-notif' href='"+item.url+"'><img class='pull-left' src='"+item.gambar+"'><span class='sender'>"+item.sender+"</span><br /><p>"+item.subjek+"</p><span class='pull-right'>"+item.tgl+"</span><br/></a></li>");
            }
        });
    });
}

function cekNotif(){
    $.getJSON('includes/sso/cek-notif.php?act=2', function(data) {
        $.each(data,function(i,item){
            if(item.banyakNotif>0){
                $('#jum-notif').addClass('badge');
                $('#jum-notif').addClass('badge-important');
                $('#jum-notif').html(item.banyakNotif);
                cekListNotif();
            }else{
                $('#jum-notif').removeClass('badge');
                $('#jum-notif').removeClass('badge-important');
                $('#jum-notif').html('');
            }
        });
    });
}

function initPesan(){
    $('#list-msg').html('');
    $.getJSON('includes/sso/cek-pesan.php?act=1', function(data) {
        $.each(data,function(i,item){
            var classNotifNotif = "";
            if(item.statusNotif=="0"){
                classNotifNotif = "class='blm-baca'";
            }
            $('#list-msg').append("<li "+classNotifNotif+" data-msg="+item.idUser+"><a class='item-notif' href='"+item.url+"'><img class='pull-left' src='"+item.gambar+"'><span class='sender'>"+item.sender+"</span><br /><p>"+item.subjek+"</p><span class='pull-right'>"+item.tgl+"</span><br/></a></li>");
        });
        setInterval('cekPesan()', 5000);
    });
}
function cekListPesan(){
    $.getJSON('includes/sso/cek-pesan.php?act=3', function(data) {
        $.each(data,function(i,item){
            var classNotifNotif = "";
            if(item.statusNotif=="0"){
                classNotifNotif = "class='blm-baca'";
            }
            var curNotif = $('#list-msg').find('li[data-msg='+item.idUser+']');
            if (curNotif.length < 1) {
                $('#list-msg').prepend("<li "+classNotifNotif+" data-msg="+item.idUser+"><a class='item-notif' href='"+item.url+"'><img class='pull-left' src='"+item.gambar+"'><span class='sender'>"+item.sender+"</span><br /><p>"+item.subjek+"</p><span class='pull-right'>"+item.tgl+"</span><br/></a></li>");
            }else{
                curNotif.addClass("blm-baca").find("p").html(item.subjek);
                curNotif.find("span:last").html(item.tgl);
            }
        });
    });
}

function cekPesan(){
    $.getJSON('includes/sso/cek-pesan.php?act=2', function(data) {
        $.each(data,function(i,item){   
            if(item.banyakPesan>0){
                $('#jum-msg').addClass('badge');
                $('#jum-msg').addClass('badge-important');
                $('#jum-msg').html(item.banyakPesan);
                cekListPesan();
            }else{
                $('#jum-msg').removeClass('badge');
                $('#jum-msg').removeClass('badge-important');
                $('#jum-msg').html('');
            }
        });
    });
}

$("#list-notif").ready(function() {
    initNotif();
});
$("#list-pesan").ready(function() {
    initPesan();
});

$(document).ready(function(){
    //toggle login focus
    $('#login-toggle').click(function(){
        $('#user_username').focus();
    });
    
    //login function
    $('#login-form').submit(function(){
        var formLogin = {
            username: $(this).find('#user_username').val(),
            password: $(this).find('#user_password').val()
        };
        var urlLogin = "includes/sso/login-logout.php?op=login";
        $.ajax({
            type: "POST",
            url: urlLogin,
            data: formLogin,
            success: function(msg){
                if(msg=='sukses'){
                    location.reload();                            
                }
            } 
        });    
        return false;
    });
    
    //logout function
    // $("#logout").click(function(){ 
    //     var urlLogout = "http://seo.com/includes/login-logout.php?op=logout";
    //     $.ajax({
    //         url: urlLogout,
    //         cache: false,
    //         success: function(msg){
    //             if(msg=="sukses"){
    //                 location.reload();
    //             }
    //         } 
    //     }); 
    //     return false;
    // });
    
    //select role
    $('.role-select').live('click', function(){
      if(!$(this).parent('li').hasClass('disabled')){
        var formRole = {
            role: $(this).attr('rel')
        };
        var urlRole = "includes/sso/act-session.php?act=1";
        $.ajax({
            type: "POST",
            url: urlRole, 
            data: formRole,
            cache: false, 
            success: function(msg){
                location.href = "http://"+window.location.host+"/";
            } 
        });
      }
      return false;
    });

    $('#page-title').prepend("<i class='"+$('.list-menu.active a i').attr("class")+"'></i> ");


    $('#searh-sidebar').keyup(function(){
        var keySearch = new RegExp($(this).val().toUpperCase());
        $('.list-menu').each(function(){
            var tempMenu = $(this).children('a').html().toUpperCase();
            var menu = $(this);
            var dropdown = menu.parent("ul");
            var visibleLength = dropdown.find(".list-menu:visible").length;
            if(keySearch.test(tempMenu)){
                if (dropdown.parent("li").is(".side-dropdown") && dropdown.css("display")=="none") {
                    dropdown.click();
                };
                menu.show();
            }else{
                if (dropdown.parent("li").is(".side-dropdown") && dropdown.css("display")!="none" && visibleLength < 1) {
                    dropdown.click();
                };
                menu.hide();
            }
        });
        $('.side-dropdown ul').each(function(){
            var elemLength = $(this).find(".list-menu").length;
            var hiddenLength = $(this).find(".list-menu:hidden").length;
            if (hiddenLength == elemLength) {
                // $(this).closest("li").hide();
            }else{
                $(this).closest("li").show();
            }
        });
    });

    $('#search-rightbar').keyup(function(){
        var keySearch = new RegExp($(this).val().toUpperCase());
        $('.sel-sim').each(function(){
            var tempMenu = $(this).html().toUpperCase();
            var menu = $(this);
            if(keySearch.test(tempMenu)){
                menu.show();
            }else{
                menu.hide();
            }
        });
    });

    //pindah sim
    /*$('.pindah-sim').live('click',function(){
        var urlSim = $(this).attr('href');
        var idSim = $(this).attr('rel');
        $.cookie('c_sim_g', idSim, { expires: 7, path: '/', domain: 'seo.com' });
        location.href = urlSim;
        return false;
    });*/

    if ($('.sidebar-nav-fixed').length>0) {

        var activeMenu = $('.sidebar-nav-fixed').find(".active").parent("ul");
        if (activeMenu.parent("li").is(".side-dropdown")) {
            activeMenu.parent("li").find(".sign").removeClass("icon-chevron-right");
            activeMenu.parent("li").find(".sign").addClass("icon-chevron-down");
            activeMenu.show();
        };
        
        var isi = $('.sidebar-nav-fixed').find(".active").find("a").contents().filter(function() {
                    return this.nodeType == 3;
                }).text();
        $('title').append(" - "+isi);

        $('.sidebar-nav-fixed').jScrollPane({
            mouseWheelSpeed: 50,
            autoReinitialise: true
        });
        $('body').jScrollPane({
            mouseWheelSpeed: 50,
            autoReinitialise: true
        });

        var api = $('.sidebar-nav-fixed').data('jsp');
        var apiBody = $('body').data('jsp');
        var throttleTimeout;
        $(window).bind(
            'resize',
            function()
            {
                if ($.browser.msie) {
                    // IE fires multiple resize events while you are dragging the browser window which
                    // causes it to crash if you try to update the scrollpane on every one. So we need
                    // to throttle it to fire a maximum of once every 50 milliseconds...
                    if (!throttleTimeout) {
                        throttleTimeout = setTimeout(
                            function()
                            {
                                api.reinitialise();
                                apiBody.reinitialise();
                                throttleTimeout = null;
                            },
                            50
                        );
                    }
                } else {
                    api.reinitialise();
                    apiBody.reinitialise();
                }
            }
        );
    };

    $('#sidebar-sso').on('click', '.side-dropdown', function(){
        var tes = $(this);
        var dropdown = tes.find("ul");
        $('.side-dropdown').each(function(){
            if ($(this).find("ul").css('display')!='none') {
                $(this).find(".sign").removeClass("icon-chevron-down");
                $(this).find(".sign").addClass("icon-chevron-right");
                $(this).find("ul").slideUp("slow", "easeOutCirc", function(){
                    api.reinitialise();
                });
            }
        });
        if (dropdown.css('display')!='none') {
            tes.find(".sign").removeClass("icon-chevron-down");
            tes.find(".sign").addClass("icon-chevron-right");
            tes.find("ul").slideUp("slow", "easeOutCirc", function(){
                api.reinitialise();
            });
        }else{
            tes.find(".sign").removeClass("icon-chevron-right");
            tes.find(".sign").addClass("icon-chevron-down");
            tes.find("ul").slideDown("slow", "easeOutCirc", function(){
                api.reinitialise();
            });
        }
        return false;
    });

    $('.side-dropdown').on("click", ".list-menu", function(e){
        e.stopPropagation();
    });

    var sidebarHide = false;
    $('.toggle-sidebar-sso').click(function() {
      if($(this).hasClass('tes')){
        var side = $('#sidebar-sso>.sidebar-nav-fixed').css('display');
        if(side=='none'){
          $('#konten').removeClass('span12');
          $('#konten').addClass('span9');
          $('#konten').removeClass('no-sidebar');
          $('#open-sidebar-sso').stop(true,true).hide('slide', { direction: 'left' }, 500);
          sidebarHide = false;
        }
        $('#sidebar-sso').stop(true,true).animate({
          width: 'toggle'
        }, 500);
        if(side!='none'){
            $('#konten').removeClass('span9');
            $('#konten').addClass('span12');
            $('#konten').addClass('no-sidebar');
            $('#open-sidebar-sso').stop(true,true).show('slide', { direction: 'left' }, 500);
        }
        $('#sidebar-sso>.sidebar-nav-fixed').stop(true,true).toggle('slide', { direction: 'left' }, 500);
      }
      $('.toggle-sidebar-sso').addClass('tes');
      $(this).removeClass('tes');
      return false;
    });
     
});